{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch as t\n",
    "import sys\n",
    "sys.path.append('../')\n",
    "from utils import get_score\n",
    "import glob\n",
    "import torch as t \n",
    "import numpy as np\n",
    "import json\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "label_path =   '/home/a/code/pytorch/zhihu/ddd/labels.json'\n",
    "test_data_path='/home/a/code/pytorch/zhihu/ddd/test.npz' \n",
    "index2qid = np.load(test_data_path)['index2qid'].item()\n",
    "with open(label_path) as f: \n",
    "      labels_info = json.load(f)\n",
    "qid2label = labels_info['d']\n",
    "label2qid = labels_info['id2label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/LSTMText0.4031_char_test.pth\n",
      "../checkpoints/result/RCNN_0.4037_char_test.pth\n",
      "../checkpoints/result/DeepText0.4103_word_test.pth\n",
      "../checkpoints/result/FastText3_word_0.409160_test.pth\n",
      "../checkpoints/result/CNNText_tmp0.4109_word_test.pth\n",
      "../checkpoints/result/LSTMText0.4119_word_test.pth\n",
      "../checkpoints/result/RCNN_0.4115_word_test.pth\n",
      "../checkpoints/result/CNNText_tmp0.4024_char_test.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/*test.pth')\n",
    "r2=0\n",
    "for file in files:\n",
    "    if 'MultiModel' not in file:\n",
    "        print file\n",
    "        r2+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/LSTMText0.41368_aug_word_test.pth\n",
      "../checkpoints/result/tmp/RCNN0.41344_aug_word_test.pth\n",
      "../checkpoints/result/tmp/FastText3_word_weight1_0.40853_test.pth\n",
      "../checkpoints/result/tmp/RCNN0.39854_aug_char_test.pth\n",
      "../checkpoints/result/tmp/MultiCNNTextBNDeep_aug_char_0.38738_test.pth\n",
      "../checkpoints/result/tmp/DeepText_word_0.410051_aug_test.pth\n",
      "../checkpoints/result/tmp/inception0.41254_aug_word_test.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*test.pth')\n",
    "r_aug=0\n",
    "for file in files:\n",
    "    if 'MultiModel' not in file and 'weight5' not in file:\n",
    "        print file\n",
    "        r_aug+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/MultiModel_0.41987_word_test.pth\n",
      "../checkpoints/result/tmp/MultiModel2w2c_0.4213_test.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.42169_test.pth\n",
      "../checkpoints/result/tmp/MultiModelall_0.4235_aug_test.pth\n",
      "../checkpoints/result/tmp/MultiModelallfast_0.419088_aug_test.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.4171859_test.pth\n",
      "../checkpoints/result/tmp/MultiModel2_0.42560_test.pth\n",
      "../checkpoints/result/tmp/MultiModelallfast_0.41652_aug_test.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*test.pth')\n",
    "r_multi=0\n",
    "for file in files:\n",
    "    if 'MultiModel' in file :\n",
    "        print file\n",
    "        r_multi+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/RCNN_0.41226_weight5_test.pth\n",
      "../checkpoints/result/tmp/MultiCNNTextBNDeep_word_weight5_0.409409_test.pth\n",
      "../checkpoints/result/tmp/LSTMText_0.41230_weight5_test.pth\n",
      "../checkpoints/result/tmp/inception_0.409912_weight5_test.pth\n",
      "../checkpoints/result/tmp/FastText3_word_weight5_0.407992_test.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*test.pth')\n",
    "r_5=0\n",
    "for file in files:\n",
    "    if 'weight5' in file :\n",
    "        print file\n",
    "        r_5+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = (r2+r_aug*1.15+r_multi*13+r_5*0.15)\n",
    "result=(tmp).topk(5,1)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 手动调整权重\n",
    "# tmp = tmp-t.load('../checkpoints/result/DeepText0.4103_word_test.pth')\n",
    "# result=(tmp-t.load('../checkpoints/result/tmp/RCNN_0.41226_weight5_test.pth')*0.005).topk(5,1)[1]\n",
    "# tmp = tmp+t.load('../checkpoints/result/DeepText0.4103_word_test.pth')*0.2\n",
    "# tmp = tmp-t.load('../checkpoints/result/tmp/LSTMText0.41368_aug_word_test.pth')*0.1\n",
    "# tmp = tmp-t.load('../checkpoints/result/CNNText_tmp0.4024_char_test.pth')*0.05\n",
    "# tmp = tmp-t.load('../checkpoints/result/tmp/MultiCNNTextBNDeep_word_weight5_0.409409_test.pth')*0.009"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 写csv 提交结果\n",
    "rows = range(result.size(0))\n",
    "for ii,item in enumerate(result):\n",
    "    rows[ii] = [index2qid[ii]] + [label2qid[str(_)] for _ in item ]\n",
    "import csv\n",
    "with open('final-0.43081.csv','w') as f:\n",
    "    writer = csv.writer(f)\n",
    "    writer.writerows(rows)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
